#include <stdio.h>
#include <math.h>
#include "rkvector.h"

int main(int args, char **argv)
{
  RkVector_t * vec;
  int i;
  int cnt[3];
  
  vec = RkVector_alloc(3, 3.0, 4.5, 6.0);
  
  if ((RkVector_ELEM(vec,0) != 3.0) || (RkVector_ELEM(vec,1) != 4.5)
      || (RkVector_ELEM(vec,2) != 6.0))
    return 1;

  if (13.5 != RkVector_sum(vec))
    return 2;
  
  RkVector_free(vec);
  
  vec = RkVector_alloc(3, .1, .6, .3);
  cnt[0] = cnt[1] = cnt[2] = 0;
  for (i=0; i<10000; i++)
    cnt[RkVector_sample(vec)] ++;

  if ((fabs(.1 - ((double) cnt[0])/10000) > 1e-2)
      || (fabs(.6 - ((double) cnt[1])/10000) > 1e-2)
      || (fabs(.3 - ((double) cnt[2])/10000) > 1e-2))
    return 3;

  RkVector_free(vec);
  
  vec = RkVector_alloc(2, .5, .5);
  cnt[0] = cnt[1] = 0;
  for (i=0; i<10000; i++)
    cnt[RkVector_sample(vec)] ++;

  if ((fabs(.5 - ((double) cnt[0])/10000) > 1e-2)
      || (fabs(.5 - ((double) cnt[1])/10000) > 1e-2))
    return 13;

  RkVector_free(vec);

  vec = RkVector_alloc(3, -10.0, -20.0, -30.0);

  RkVector_lognormalize(vec);
  
  if (fabs(RkVector_ELEM(vec, 0) - 1) > 1e-3)
    return 4;

  RkVector_free(vec);
  
  vec = RkVector_alloc(3, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL);

  RkVector_lognormalize(vec);
  
  if (fabs(RkVector_ELEM(vec, 0) - (1.0/3.0)) > 1e-6)
    return 5;

  RkVector_free(vec);

  vec = RkVector_alloc(5, HUGE_VAL, -10.0, -HUGE_VAL, HUGE_VAL, 1.0);

  RkVector_lognormalize(vec);
  
  if (fabs(RkVector_ELEM(vec, 0) - .5) > 1e-6)
    return 6;

  if (fabs(RkVector_ELEM(vec, 2) - 0) > 1e-6)
    return 7;

  RkVector_free(vec);
  
  vec = RkVector_alloc(3, -HUGE_VAL, -1.0, -1.0);

  RkVector_lognormalize(vec);

  if (fabs(RkVector_ELEM(vec, 0) - 0) > 1e-6)
    return 8;

  if (fabs(RkVector_ELEM(vec, 1) - 0.5) > 1e-3)
    return 9;

  return 0;
}
